package com.intct.hbase.ddl.table;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.*;

/**
 * @author gufg
 * @since 2025-08-05 10:45
 */
public class CreateTable {
    public static void main(String[] args) throws IOException {
        // 创建Hbase连接
        Connection connection = ConnectionFactory.createConnection();

        // 获取Admin实例
        Admin admin = connection.getAdmin();

        // 判断表是否存在
        TableName tableName = TableName.valueOf("intct83", "t5_api");

        // f1列族，设置为3版本；f2列族，设置为5版本  ==> json string
//        List<String> familyNameList = Arrays.asList("f1", "f2");
        String jsonStr = getJsonString();
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);

        List<ColumnFamilyDescriptor> familyList = new ArrayList<>();

        try {
            // 判断表不存时，进行创建
            if (!admin.tableExists(tableName)) {
                // 表的描述
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

                for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
                    String family = entry.getKey();
                    Integer version = Integer.valueOf(String.valueOf(entry.getValue()));
                    // 列族描述
                    ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(family.getBytes());

                    // 设置版本参数
                    columnFamilyDescriptorBuilder.setMaxVersions(version);

                    familyList.add(columnFamilyDescriptorBuilder.build());
                }
//                for (String family : familyNameList) {
//                    // 列族描述
//                    ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(family.getBytes());
//
//                    // 设置版本参数
//                    columnFamilyDescriptorBuilder.setMaxVersions(3);
//
//                    familyList.add(columnFamilyDescriptorBuilder.build());
//                }

                // 设置列族
                tableDescriptorBuilder.setColumnFamilies(familyList);

                // 预分区
                byte[][] splitKeys = new byte[4][];
                splitKeys[0] = "10".getBytes();
                splitKeys[1] = "20".getBytes();
                splitKeys[2] = "30".getBytes();
                splitKeys[3] = "40".getBytes();

                // 创建表
                admin.createTable(tableDescriptorBuilder.build(), splitKeys);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (admin != null) {
                admin.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

    }

    private static String getJsonString(){
        Map<String, String> familyMap = new HashMap<>();
        familyMap.put("f1", "3");
        familyMap.put("f2", "5");
        familyMap.put("f3", "5");
//
//        for (Map.Entry<String, String> entry : familyMap.entrySet()) {
//            entry.getKey();
//            entry.getValue()
//        }

//        String jsonStr = "{f1:3,f2:5}";
//        System.out.println(JSON.parseObject(jsonStr, Map.class) instanceof Map);
//
//        JSONObject jsonObj = JSONObject.parseObject(jsonStr);


        return JSON.toJSONString(familyMap);
    }
}
